package comment_pb

import (
	context "context"
	fmt "fmt"
	gorm1 "github.com/infobloxopen/atlas-app-toolkit/gorm"
	query "github.com/infobloxopen/atlas-app-toolkit/query"
	errors "github.com/infobloxopen/protoc-gen-gorm/errors"
	gorm "github.com/jinzhu/gorm"
	field_mask "google.golang.org/genproto/protobuf/field_mask"
	timestamppb "google.golang.org/protobuf/types/known/timestamppb"
	strings "strings"
	time "time"
)

type CommentORM struct {
	Content   string
	CreatedAt *time.Time
	Id        uint64
	PostId    uint64
	UpdatedAt *time.Time
	UserId    uint64
	Uuid      string
}

// TableName overrides the default tablename generated by GORM
func (CommentORM) TableName() string {
	return "comments"
}

// ToORM runs the BeforeToORM hook if present, converts the fields of this
// object to ORM format, runs the AfterToORM hook, then returns the ORM object
func (m *Comment) ToORM(ctx context.Context) (CommentORM, error) {
	to := CommentORM{}
	var err error
	if prehook, ok := interface{}(m).(CommentWithBeforeToORM); ok {
		if err = prehook.BeforeToORM(ctx, &to); err != nil {
			return to, err
		}
	}
	to.Id = m.Id
	if m.CreatedAt != nil {
		t := m.CreatedAt.AsTime()
		to.CreatedAt = &t
	}
	if m.UpdatedAt != nil {
		t := m.UpdatedAt.AsTime()
		to.UpdatedAt = &t
	}
	to.Content = m.Content
	to.UserId = m.UserId
	to.Uuid = m.Uuid
	to.PostId = m.PostId
	if posthook, ok := interface{}(m).(CommentWithAfterToORM); ok {
		err = posthook.AfterToORM(ctx, &to)
	}
	return to, err
}

// ToPB runs the BeforeToPB hook if present, converts the fields of this
// object to PB format, runs the AfterToPB hook, then returns the PB object
func (m *CommentORM) ToPB(ctx context.Context) (Comment, error) {
	to := Comment{}
	var err error
	if prehook, ok := interface{}(m).(CommentWithBeforeToPB); ok {
		if err = prehook.BeforeToPB(ctx, &to); err != nil {
			return to, err
		}
	}
	to.Id = m.Id
	if m.CreatedAt != nil {
		to.CreatedAt = timestamppb.New(*m.CreatedAt)
	}
	if m.UpdatedAt != nil {
		to.UpdatedAt = timestamppb.New(*m.UpdatedAt)
	}
	to.Content = m.Content
	to.UserId = m.UserId
	to.Uuid = m.Uuid
	to.PostId = m.PostId
	if posthook, ok := interface{}(m).(CommentWithAfterToPB); ok {
		err = posthook.AfterToPB(ctx, &to)
	}
	return to, err
}

// The following are interfaces you can implement for special behavior during ORM/PB conversions
// of type Comment the arg will be the target, the caller the one being converted from

// CommentBeforeToORM called before default ToORM code
type CommentWithBeforeToORM interface {
	BeforeToORM(context.Context, *CommentORM) error
}

// CommentAfterToORM called after default ToORM code
type CommentWithAfterToORM interface {
	AfterToORM(context.Context, *CommentORM) error
}

// CommentBeforeToPB called before default ToPB code
type CommentWithBeforeToPB interface {
	BeforeToPB(context.Context, *Comment) error
}

// CommentAfterToPB called after default ToPB code
type CommentWithAfterToPB interface {
	AfterToPB(context.Context, *Comment) error
}

// DefaultCreateComment executes a basic gorm create call
func DefaultCreateComment(ctx context.Context, in *Comment, db *gorm.DB) (*Comment, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(CommentORMWithBeforeCreate_); ok {
		if db, err = hook.BeforeCreate_(ctx, db); err != nil {
			return nil, err
		}
	}
	if err = db.Create(&ormObj).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(CommentORMWithAfterCreate_); ok {
		if err = hook.AfterCreate_(ctx, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormObj.ToPB(ctx)
	return &pbResponse, err
}

type CommentORMWithBeforeCreate_ interface {
	BeforeCreate_(context.Context, *gorm.DB) (*gorm.DB, error)
}
type CommentORMWithAfterCreate_ interface {
	AfterCreate_(context.Context, *gorm.DB) error
}

func DefaultReadComment(ctx context.Context, in *Comment, db *gorm.DB, fs *query.FieldSelection) (*Comment, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if ormObj.Id == 0 {
		return nil, errors.EmptyIdError
	}
	if hook, ok := interface{}(&ormObj).(CommentORMWithBeforeReadApplyQuery); ok {
		if db, err = hook.BeforeReadApplyQuery(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	if db, err = gorm1.ApplyFieldSelection(ctx, db, fs, &CommentORM{}); err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(CommentORMWithBeforeReadFind); ok {
		if db, err = hook.BeforeReadFind(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	ormResponse := CommentORM{}
	if err = db.Where(&ormObj).First(&ormResponse).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormResponse).(CommentORMWithAfterReadFind); ok {
		if err = hook.AfterReadFind(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormResponse.ToPB(ctx)
	return &pbResponse, err
}

type CommentORMWithBeforeReadApplyQuery interface {
	BeforeReadApplyQuery(context.Context, *gorm.DB, *query.FieldSelection) (*gorm.DB, error)
}
type CommentORMWithBeforeReadFind interface {
	BeforeReadFind(context.Context, *gorm.DB, *query.FieldSelection) (*gorm.DB, error)
}
type CommentORMWithAfterReadFind interface {
	AfterReadFind(context.Context, *gorm.DB, *query.FieldSelection) error
}

func DefaultDeleteComment(ctx context.Context, in *Comment, db *gorm.DB) error {
	if in == nil {
		return errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return err
	}
	if ormObj.Id == 0 {
		return errors.EmptyIdError
	}
	if hook, ok := interface{}(&ormObj).(CommentORMWithBeforeDelete_); ok {
		if db, err = hook.BeforeDelete_(ctx, db); err != nil {
			return err
		}
	}
	err = db.Where(&ormObj).Delete(&CommentORM{}).Error
	if err != nil {
		return err
	}
	if hook, ok := interface{}(&ormObj).(CommentORMWithAfterDelete_); ok {
		err = hook.AfterDelete_(ctx, db)
	}
	return err
}

type CommentORMWithBeforeDelete_ interface {
	BeforeDelete_(context.Context, *gorm.DB) (*gorm.DB, error)
}
type CommentORMWithAfterDelete_ interface {
	AfterDelete_(context.Context, *gorm.DB) error
}

func DefaultDeleteCommentSet(ctx context.Context, in []*Comment, db *gorm.DB) error {
	if in == nil {
		return errors.NilArgumentError
	}
	var err error
	keys := []uint64{}
	for _, obj := range in {
		ormObj, err := obj.ToORM(ctx)
		if err != nil {
			return err
		}
		if ormObj.Id == 0 {
			return errors.EmptyIdError
		}
		keys = append(keys, ormObj.Id)
	}
	if hook, ok := (interface{}(&CommentORM{})).(CommentORMWithBeforeDeleteSet); ok {
		if db, err = hook.BeforeDeleteSet(ctx, in, db); err != nil {
			return err
		}
	}
	err = db.Where("id in (?)", keys).Delete(&CommentORM{}).Error
	if err != nil {
		return err
	}
	if hook, ok := (interface{}(&CommentORM{})).(CommentORMWithAfterDeleteSet); ok {
		err = hook.AfterDeleteSet(ctx, in, db)
	}
	return err
}

type CommentORMWithBeforeDeleteSet interface {
	BeforeDeleteSet(context.Context, []*Comment, *gorm.DB) (*gorm.DB, error)
}
type CommentORMWithAfterDeleteSet interface {
	AfterDeleteSet(context.Context, []*Comment, *gorm.DB) error
}

// DefaultStrictUpdateComment clears / replaces / appends first level 1:many children and then executes a gorm update call
func DefaultStrictUpdateComment(ctx context.Context, in *Comment, db *gorm.DB) (*Comment, error) {
	if in == nil {
		return nil, fmt.Errorf("Nil argument to DefaultStrictUpdateComment")
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	lockedRow := &CommentORM{}
	db.Model(&ormObj).Set("gorm:query_option", "FOR UPDATE").Where("id=?", ormObj.Id).First(lockedRow)
	if hook, ok := interface{}(&ormObj).(CommentORMWithBeforeStrictUpdateCleanup); ok {
		if db, err = hook.BeforeStrictUpdateCleanup(ctx, db); err != nil {
			return nil, err
		}
	}
	if hook, ok := interface{}(&ormObj).(CommentORMWithBeforeStrictUpdateSave); ok {
		if db, err = hook.BeforeStrictUpdateSave(ctx, db); err != nil {
			return nil, err
		}
	}
	if err = db.Save(&ormObj).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(CommentORMWithAfterStrictUpdateSave); ok {
		if err = hook.AfterStrictUpdateSave(ctx, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormObj.ToPB(ctx)
	if err != nil {
		return nil, err
	}
	return &pbResponse, err
}

type CommentORMWithBeforeStrictUpdateCleanup interface {
	BeforeStrictUpdateCleanup(context.Context, *gorm.DB) (*gorm.DB, error)
}
type CommentORMWithBeforeStrictUpdateSave interface {
	BeforeStrictUpdateSave(context.Context, *gorm.DB) (*gorm.DB, error)
}
type CommentORMWithAfterStrictUpdateSave interface {
	AfterStrictUpdateSave(context.Context, *gorm.DB) error
}

// DefaultPatchComment executes a basic gorm update call with patch behavior
func DefaultPatchComment(ctx context.Context, in *Comment, updateMask *field_mask.FieldMask, db *gorm.DB) (*Comment, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	var pbObj Comment
	var err error
	if hook, ok := interface{}(&pbObj).(CommentWithBeforePatchRead); ok {
		if db, err = hook.BeforePatchRead(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	pbReadRes, err := DefaultReadComment(ctx, &Comment{Id: in.GetId()}, db, nil)
	if err != nil {
		return nil, err
	}
	pbObj = *pbReadRes
	if hook, ok := interface{}(&pbObj).(CommentWithBeforePatchApplyFieldMask); ok {
		if db, err = hook.BeforePatchApplyFieldMask(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	if _, err := DefaultApplyFieldMaskComment(ctx, &pbObj, in, updateMask, "", db); err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&pbObj).(CommentWithBeforePatchSave); ok {
		if db, err = hook.BeforePatchSave(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := DefaultStrictUpdateComment(ctx, &pbObj, db)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(pbResponse).(CommentWithAfterPatchSave); ok {
		if err = hook.AfterPatchSave(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	return pbResponse, nil
}

type CommentWithBeforePatchRead interface {
	BeforePatchRead(context.Context, *Comment, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type CommentWithBeforePatchApplyFieldMask interface {
	BeforePatchApplyFieldMask(context.Context, *Comment, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type CommentWithBeforePatchSave interface {
	BeforePatchSave(context.Context, *Comment, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type CommentWithAfterPatchSave interface {
	AfterPatchSave(context.Context, *Comment, *field_mask.FieldMask, *gorm.DB) error
}

// DefaultPatchSetComment executes a bulk gorm update call with patch behavior
func DefaultPatchSetComment(ctx context.Context, objects []*Comment, updateMasks []*field_mask.FieldMask, db *gorm.DB) ([]*Comment, error) {
	if len(objects) != len(updateMasks) {
		return nil, fmt.Errorf(errors.BadRepeatedFieldMaskTpl, len(updateMasks), len(objects))
	}

	results := make([]*Comment, 0, len(objects))
	for i, patcher := range objects {
		pbResponse, err := DefaultPatchComment(ctx, patcher, updateMasks[i], db)
		if err != nil {
			return nil, err
		}

		results = append(results, pbResponse)
	}

	return results, nil
}

// DefaultApplyFieldMaskComment patches an pbObject with patcher according to a field mask.
func DefaultApplyFieldMaskComment(ctx context.Context, patchee *Comment, patcher *Comment, updateMask *field_mask.FieldMask, prefix string, db *gorm.DB) (*Comment, error) {
	if patcher == nil {
		return nil, nil
	} else if patchee == nil {
		return nil, errors.NilArgumentError
	}
	var err error
	var updatedCreatedAt bool
	var updatedUpdatedAt bool
	for i, f := range updateMask.Paths {
		if f == prefix+"Id" {
			patchee.Id = patcher.Id
			continue
		}
		if !updatedCreatedAt && strings.HasPrefix(f, prefix+"CreatedAt.") {
			if patcher.CreatedAt == nil {
				patchee.CreatedAt = nil
				continue
			}
			if patchee.CreatedAt == nil {
				patchee.CreatedAt = &timestamppb.Timestamp{}
			}
			childMask := &field_mask.FieldMask{}
			for j := i; j < len(updateMask.Paths); j++ {
				if trimPath := strings.TrimPrefix(updateMask.Paths[j], prefix+"CreatedAt."); trimPath != updateMask.Paths[j] {
					childMask.Paths = append(childMask.Paths, trimPath)
				}
			}
			if err := gorm1.MergeWithMask(patcher.CreatedAt, patchee.CreatedAt, childMask); err != nil {
				return nil, nil
			}
		}
		if f == prefix+"CreatedAt" {
			updatedCreatedAt = true
			patchee.CreatedAt = patcher.CreatedAt
			continue
		}
		if !updatedUpdatedAt && strings.HasPrefix(f, prefix+"UpdatedAt.") {
			if patcher.UpdatedAt == nil {
				patchee.UpdatedAt = nil
				continue
			}
			if patchee.UpdatedAt == nil {
				patchee.UpdatedAt = &timestamppb.Timestamp{}
			}
			childMask := &field_mask.FieldMask{}
			for j := i; j < len(updateMask.Paths); j++ {
				if trimPath := strings.TrimPrefix(updateMask.Paths[j], prefix+"UpdatedAt."); trimPath != updateMask.Paths[j] {
					childMask.Paths = append(childMask.Paths, trimPath)
				}
			}
			if err := gorm1.MergeWithMask(patcher.UpdatedAt, patchee.UpdatedAt, childMask); err != nil {
				return nil, nil
			}
		}
		if f == prefix+"UpdatedAt" {
			updatedUpdatedAt = true
			patchee.UpdatedAt = patcher.UpdatedAt
			continue
		}
		if f == prefix+"Content" {
			patchee.Content = patcher.Content
			continue
		}
		if f == prefix+"UserId" {
			patchee.UserId = patcher.UserId
			continue
		}
		if f == prefix+"Uuid" {
			patchee.Uuid = patcher.Uuid
			continue
		}
		if f == prefix+"PostId" {
			patchee.PostId = patcher.PostId
			continue
		}
	}
	if err != nil {
		return nil, err
	}
	return patchee, nil
}

// DefaultListComment executes a gorm list call
func DefaultListComment(ctx context.Context, db *gorm.DB, f *query.Filtering, s *query.Sorting, p *query.Pagination, fs *query.FieldSelection) ([]*Comment, error) {
	in := Comment{}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(CommentORMWithBeforeListApplyQuery); ok {
		if db, err = hook.BeforeListApplyQuery(ctx, db, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	db, err = gorm1.ApplyCollectionOperators(ctx, db, &CommentORM{}, &Comment{}, f, s, p, fs)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(CommentORMWithBeforeListFind); ok {
		if db, err = hook.BeforeListFind(ctx, db, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	db = db.Where(&ormObj)
	db = db.Order("id")
	ormResponse := []CommentORM{}
	if err := db.Find(&ormResponse).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(CommentORMWithAfterListFind); ok {
		if err = hook.AfterListFind(ctx, db, &ormResponse, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	pbResponse := []*Comment{}
	for _, responseEntry := range ormResponse {
		temp, err := responseEntry.ToPB(ctx)
		if err != nil {
			return nil, err
		}
		pbResponse = append(pbResponse, &temp)
	}
	return pbResponse, nil
}

type CommentORMWithBeforeListApplyQuery interface {
	BeforeListApplyQuery(context.Context, *gorm.DB, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) (*gorm.DB, error)
}
type CommentORMWithBeforeListFind interface {
	BeforeListFind(context.Context, *gorm.DB, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) (*gorm.DB, error)
}
type CommentORMWithAfterListFind interface {
	AfterListFind(context.Context, *gorm.DB, *[]CommentORM, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) error
}
type CommentServiceDefaultServer struct {
	DB *gorm.DB
}

// CreateCompensate ...
func (m *CommentServiceDefaultServer) CreateCompensate(ctx context.Context, in *CreateCommentRequest) (*CreateCommentResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(CommentServiceCommentWithBeforeCreateCompensate); ok {
		var err error
		if db, err = custom.BeforeCreateCompensate(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err := DefaultCreateComment(ctx, in.GetPayload(), db)
	if err != nil {
		return nil, err
	}
	out := &CreateCommentResponse{Result: res}
	if custom, ok := interface{}(in).(CommentServiceCommentWithAfterCreateCompensate); ok {
		var err error
		if err = custom.AfterCreateCompensate(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// CommentServiceCommentWithBeforeCreateCompensate called before DefaultCreateCompensateComment in the default CreateCompensate handler
type CommentServiceCommentWithBeforeCreateCompensate interface {
	BeforeCreateCompensate(context.Context, *gorm.DB) (*gorm.DB, error)
}

// CommentServiceCommentWithAfterCreateCompensate called before DefaultCreateCompensateComment in the default CreateCompensate handler
type CommentServiceCommentWithAfterCreateCompensate interface {
	AfterCreateCompensate(context.Context, *CreateCommentResponse, *gorm.DB) error
}

// GetListByPostId ...
func (m *CommentServiceDefaultServer) GetListByPostId(ctx context.Context, in *GetListByPostIdRequest) (*GetListByPostIdResponse, error) {
	out := &GetListByPostIdResponse{}
	return out, nil
}

// Create ...
func (m *CommentServiceDefaultServer) Create(ctx context.Context, in *CreateCommentRequest) (*CreateCommentResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(CommentServiceCommentWithBeforeCreate); ok {
		var err error
		if db, err = custom.BeforeCreate(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err := DefaultCreateComment(ctx, in.GetPayload(), db)
	if err != nil {
		return nil, err
	}
	out := &CreateCommentResponse{Result: res}
	if custom, ok := interface{}(in).(CommentServiceCommentWithAfterCreate); ok {
		var err error
		if err = custom.AfterCreate(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// CommentServiceCommentWithBeforeCreate called before DefaultCreateComment in the default Create handler
type CommentServiceCommentWithBeforeCreate interface {
	BeforeCreate(context.Context, *gorm.DB) (*gorm.DB, error)
}

// CommentServiceCommentWithAfterCreate called before DefaultCreateComment in the default Create handler
type CommentServiceCommentWithAfterCreate interface {
	AfterCreate(context.Context, *CreateCommentResponse, *gorm.DB) error
}

// Read ...
func (m *CommentServiceDefaultServer) Read(ctx context.Context, in *ReadCommentRequest) (*ReadCommentResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(CommentServiceCommentWithBeforeRead); ok {
		var err error
		if db, err = custom.BeforeRead(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err := DefaultReadComment(ctx, &Comment{Id: in.GetId()}, db, in.Fields)
	if err != nil {
		return nil, err
	}
	out := &ReadCommentResponse{Result: res}
	if custom, ok := interface{}(in).(CommentServiceCommentWithAfterRead); ok {
		var err error
		if err = custom.AfterRead(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// CommentServiceCommentWithBeforeRead called before DefaultReadComment in the default Read handler
type CommentServiceCommentWithBeforeRead interface {
	BeforeRead(context.Context, *gorm.DB) (*gorm.DB, error)
}

// CommentServiceCommentWithAfterRead called before DefaultReadComment in the default Read handler
type CommentServiceCommentWithAfterRead interface {
	AfterRead(context.Context, *ReadCommentResponse, *gorm.DB) error
}

// Update ...
func (m *CommentServiceDefaultServer) Update(ctx context.Context, in *UpdateCommentRequest) (*UpdateCommentResponse, error) {
	var err error
	var res *Comment
	db := m.DB
	if custom, ok := interface{}(in).(CommentServiceCommentWithBeforeUpdate); ok {
		var err error
		if db, err = custom.BeforeUpdate(ctx, db); err != nil {
			return nil, err
		}
	}
	if in.GetGerogeriGegege() == nil {
		res, err = DefaultStrictUpdateComment(ctx, in.GetPayload(), db)
	} else {
		res, err = DefaultPatchComment(ctx, in.GetPayload(), in.GetGerogeriGegege(), db)
	}
	if err != nil {
		return nil, err
	}
	out := &UpdateCommentResponse{Result: res}
	if custom, ok := interface{}(in).(CommentServiceCommentWithAfterUpdate); ok {
		var err error
		if err = custom.AfterUpdate(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// CommentServiceCommentWithBeforeUpdate called before DefaultUpdateComment in the default Update handler
type CommentServiceCommentWithBeforeUpdate interface {
	BeforeUpdate(context.Context, *gorm.DB) (*gorm.DB, error)
}

// CommentServiceCommentWithAfterUpdate called before DefaultUpdateComment in the default Update handler
type CommentServiceCommentWithAfterUpdate interface {
	AfterUpdate(context.Context, *UpdateCommentResponse, *gorm.DB) error
}

// UpdateSet ...
func (m *CommentServiceDefaultServer) UpdateSet(ctx context.Context, in *UpdateSetCommentRequest) (*UpdateSetCommentResponse, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}

	db := m.DB

	if custom, ok := interface{}(in).(CommentServiceCommentWithBeforeUpdateSet); ok {
		var err error
		if db, err = custom.BeforeUpdateSet(ctx, db); err != nil {
			return nil, err
		}
	}

	res, err := DefaultPatchSetComment(ctx, in.GetObjects(), in.GetMasks(), db)
	if err != nil {
		return nil, err
	}

	out := &UpdateSetCommentResponse{Results: res}

	if custom, ok := interface{}(in).(CommentServiceCommentWithAfterUpdateSet); ok {
		var err error
		if err = custom.AfterUpdateSet(ctx, out, db); err != nil {
			return nil, err
		}
	}

	return out, nil
}

// CommentServiceCommentWithBeforeUpdateSet called before DefaultUpdateSetComment in the default UpdateSet handler
type CommentServiceCommentWithBeforeUpdateSet interface {
	BeforeUpdateSet(context.Context, *gorm.DB) (*gorm.DB, error)
}

// CommentServiceCommentWithAfterUpdateSet called before DefaultUpdateSetComment in the default UpdateSet handler
type CommentServiceCommentWithAfterUpdateSet interface {
	AfterUpdateSet(context.Context, *UpdateSetCommentResponse, *gorm.DB) error
}

// List ...
func (m *CommentServiceDefaultServer) List(ctx context.Context, in *ListCommentRequest) (*ListCommentResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(CommentServiceCommentWithBeforeList); ok {
		var err error
		if db, err = custom.BeforeList(ctx, db); err != nil {
			return nil, err
		}
	}
	pagedRequest := false
	if in.GetPaging().GetLimit() >= 1 {
		in.Paging.Limit++
		pagedRequest = true
	}
	res, err := DefaultListComment(ctx, db, in.Filter, in.OrderBy, in.Paging, in.Fields)
	if err != nil {
		return nil, err
	}
	var resPaging *query.PageInfo
	if pagedRequest {
		var offset int32
		var size int32 = int32(len(res))
		if size == in.GetPaging().GetLimit() {
			size--
			res = res[:size]
			offset = in.GetPaging().GetOffset() + size
		}
		resPaging = &query.PageInfo{Offset: offset}
	}
	out := &ListCommentResponse{Results: res, PageInfo: resPaging}
	if custom, ok := interface{}(in).(CommentServiceCommentWithAfterList); ok {
		var err error
		if err = custom.AfterList(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// CommentServiceCommentWithBeforeList called before DefaultListComment in the default List handler
type CommentServiceCommentWithBeforeList interface {
	BeforeList(context.Context, *gorm.DB) (*gorm.DB, error)
}

// CommentServiceCommentWithAfterList called before DefaultListComment in the default List handler
type CommentServiceCommentWithAfterList interface {
	AfterList(context.Context, *ListCommentResponse, *gorm.DB) error
}

// Delete ...
func (m *CommentServiceDefaultServer) Delete(ctx context.Context, in *DeleteCommentRequest) (*DeleteCommentResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(CommentServiceCommentWithBeforeDelete); ok {
		var err error
		if db, err = custom.BeforeDelete(ctx, db); err != nil {
			return nil, err
		}
	}
	err := DefaultDeleteComment(ctx, &Comment{Id: in.GetId()}, db)
	if err != nil {
		return nil, err
	}
	out := &DeleteCommentResponse{}
	if custom, ok := interface{}(in).(CommentServiceCommentWithAfterDelete); ok {
		var err error
		if err = custom.AfterDelete(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// CommentServiceCommentWithBeforeDelete called before DefaultDeleteComment in the default Delete handler
type CommentServiceCommentWithBeforeDelete interface {
	BeforeDelete(context.Context, *gorm.DB) (*gorm.DB, error)
}

// CommentServiceCommentWithAfterDelete called before DefaultDeleteComment in the default Delete handler
type CommentServiceCommentWithAfterDelete interface {
	AfterDelete(context.Context, *DeleteCommentResponse, *gorm.DB) error
}

// DeleteSet ...
func (m *CommentServiceDefaultServer) DeleteSet(ctx context.Context, in *DeletesCommentRequest) (*DeleteCommentResponse, error) {
	db := m.DB
	objs := []*Comment{}
	for _, id := range in.Ids {
		objs = append(objs, &Comment{Id: id})
	}
	if custom, ok := interface{}(in).(CommentServiceCommentWithBeforeDeleteSet); ok {
		var err error
		if db, err = custom.BeforeDeleteSet(ctx, db); err != nil {
			return nil, err
		}
	}
	err := DefaultDeleteCommentSet(ctx, objs, db)
	if err != nil {
		return nil, err
	}
	out := &DeleteCommentResponse{}
	if custom, ok := interface{}(in).(CommentServiceCommentWithAfterDeleteSet); ok {
		var err error
		if err = custom.AfterDeleteSet(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// CommentServiceCommentWithBeforeDeleteSet called before DefaultDeleteSetComment in the default DeleteSet handler
type CommentServiceCommentWithBeforeDeleteSet interface {
	BeforeDeleteSet(context.Context, *gorm.DB) (*gorm.DB, error)
}

// CommentServiceCommentWithAfterDeleteSet called before DefaultDeleteSetComment in the default DeleteSet handler
type CommentServiceCommentWithAfterDeleteSet interface {
	AfterDeleteSet(context.Context, *DeleteCommentResponse, *gorm.DB) error
}
